const SERVICE = require("../../lib/js/service.js");
const Golbal = require("../../lib/js/global.js");

Page({
  data:{
   
  },
  onLoad(){
    let get_Date = new Date(),
      yaer = get_Date.getFullYear(),
      month = get_Date.getMonth() + 1,
      date = get_Date.getDate();
    this.setData({
      max_date: yaer + '-' + month + '-' + date
    });
  },

  /*分享*/
  onShareAppMessage(){},

  onShow(options){
    if(options){
      this.setData({
        onShowOptions: options,
        carseries_cartype: options.carSeriesName + options.carTypeName
      });
      this.getQuota({
        brand: options.brandName,
        carseries: options.seriesName,
        cartype: options.carTypeName
      });
    }
  },
  getDate(e){
    let _this = this, td = this.data, date = e.detail.value;

    wx.showLoading({
      title: "获取信息中...",
      mask: true
    });
    SERVICE.POST(SERVICE.URL.getListSet,{
      registTime: date,
      cartype: td.onShowOptions.model_name
    },(status,res)=>{
      wx.hideLoading();
      if(res.data.status === "success"){
        let data = res.data;
        if (data.result[0].code == "fail" || data.result[0].code == "noprice" || data.result[0].code =="nocartype"){
          wx.showToast({
            title: "您的爱车不在保质期内",
            icon: "none",
            mask: true
          });
        }else{
          _this.setData({
            date: date,
            service_area: data.result[0].listrange,
            deadline: data.result[0].listtimes,
            age: res.data.result[0].code
          });
        }
      }else{
        wx.showToast({
          title: "获取信息失败",
          icon: "none",
          mask: true
        });
      }
    });
  },

  /*获取服务范围*/
  getServiceArea(e){
    let td = this.data, index = e.detail.value;
    this.setData({
      service_area_val: td.service_area[index]
    });
  },

  /**
   * @explain getQuota 赔偿限额
   * @param brand
   * @param carseries
   * @param cartype
   * 
   * */ 
  getQuota(params){
    let _this = this;
    SERVICE.POST(SERVICE.URL.getprice,params, (status, res) => {
      console.log(res);
      if (res.data.status === "success") {
        _this.setData({
          data_range: res.data.data_range,
          price: res.data.result[0].number
        });

      } else {
        wx.showToast({
          title: "获取赔偿限额失败",
          icon: "none",
          mask: true
        });
      }
    });
  },

  /*获取电话*/ 
  getMobile(e){
    let mobile = e.detail.value;
    if(mobile.length > 0 && Golbal.checkPhone(mobile) === false){
      wx.showToast({
        title: "请输入正确的电话号码",
        icon: "none",
        mask: true
      });
    }
  },

  /*获取服务期限*/
  getDeadline(e){
    let td = this.data, index = e.detail.value;
    this.setData({
      deadline_val: td.deadline[index]
    });
  },

  /*获取赔偿限额*/
  getDataRange(e){
    let td = this.data, index = e.detail.value;
    this.setData({
      data_range_val: td.data_range[index].compen
    });
  },
  
  /*报价*/ 
  fromSubmit(e){
    let _this = this, td = this.data, form_obj = e.detail.value;
    if (!td.carseries_cartype){
      wx.showToast({
        title: "请选择车型",
        icon: "none",
        mask: true
      });
    }else if (!form_obj.date) {
      wx.showToast({
        title: "请选择日期",
        icon: "none",
        mask: true
      });
    } else if (!form_obj.servicearea) {
      wx.showToast({
        title: "请选择服务范围",
        icon: "none",
        mask: true
      });
    } else if (!form_obj.datarange) {
      wx.showToast({
        title: "请选择赔偿限额",
        icon: "none",
        mask: true
      });
    } else if (!form_obj.deadline) {
      wx.showToast({
        title: "请选服务期限",
        icon: "none",
        mask: true
      });
    }else{
      wx.showLoading({
        title:"报价中...",
        mask: true
      });
      SERVICE.POST(SERVICE.URL.getResult,{
        paymoney: td.price,
        age: td.age,
        ranges: form_obj.servicearea,
        compen: form_obj.datarange,
        times: form_obj.deadline
      },(status,res)=>{
        wx.hideLoading();
        if(res.data.status === "success"){
          wx.showToast({
            title: "获取报价成功",
            icon: "none",
            mask: true,
            success:function(){
              _this.setData({
                quote: res.data.result[0].omoney
              });
              // setTimeout(()=>{
              //   _this.formReset();                
              // },2000);
            }
          });
        }else{
          wx.showToast({
            title: "获取报价失败",
            icon: "none",
            mask: true
          });
        }
      });
    }
  },

  formReset(){
    this.setData({
      // quote: 0,
      carseries_cartype: null,
      date: null,
      service_area_val: null,
      data_range_val: null,
      deadline_val: null
    });
  },
  /*大师伙伴*/ 
  formSubmitInfo(e){
    let form_obj = e.detail.value;
    if(!form_obj.name){
      wx.showToast({
        title: "请输入姓名",
        icon: "none",
        mask: true
      });
    } else if (!form_obj.mobile){
      wx.showToast({
        title: "请输入电话号码",
        icon: "none",
        mask: true
      });
    } else if (form_obj.mobile.length > 0 && Golbal.checkPhone(form_obj.mobile) === false) {
      wx.showToast({
        title: "请输入正确的电话号码",
        icon: "none",
        mask: true
      });
    } else if (!form_obj.address) {
      wx.showToast({
        title: "请输入地址",
        icon: "none",
        mask: true
      });
    }else{
      wx.showLoading({
        title: "申请中...",
        mask: true
      });
      SERVICE.POST(SERVICE.URL.warranty,{
        contact_name: form_obj.name,
        contact_phone: form_obj.mobile,
        address: form_obj.address
      },(status,res)=>{
        wx.hideLoading();
        if(res.data.status === "success"){
          wx.showToast({
            title: "申请成功",
            icon: "none",
            mask: true
          });
        }else{
          wx.showToast({
            title: "申请失败",
            icon: "none",
            mask: true
          });
        }
      });
    }
  },
  getBrand(){
    wx.navigateTo({
      url: "../brand2/brand2",
    });
  },

  sos(){
    this.setData({
      ShowSOS: true
    });
  },
  qiut(){
    this.setData({
      ShowSOS: false
    });
  }
});